package com.kx.springboot.web;

import com.kx.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @RequestMapping("/each/list")
    public Object eachList(Model model) {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(101 + i);
            user.setNick("张" + i);
            user.setPhone("173200" + i);
            user.setAddress("浙江省杭州市" + i);
            userList.add(user);
        }
        model.addAttribute("userList", userList);
        return "eachList";
    }
    @RequestMapping("/each/map")
    public Object eachMap(Model model) {
        Map<Object,User> userMap = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(101 + i);
            user.setNick("张" + i);
            user.setPhone("173200" + i);
            user.setAddress("浙江省杭州市" + i);
            userMap.put(i,user);
        }
        model.addAttribute("userMap", userMap);
        return "eachMap";
    }
    @RequestMapping("/each/array")
    public Object eachArray(Model model) {
        User[] userArray =new User[10];
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(101 + i);
            user.setNick("张" + i);
            user.setPhone("173200" + i);
            user.setAddress("浙江省杭州市" + i);
            userArray[i] = user;
        }
        model.addAttribute("userArray", userArray);
        return "eachArray";
    }
    @RequestMapping("/each/all")
    public Object eachAll(Model model){
        List<Map<Integer,List<User>>> list = new ArrayList<>();
        for (int i=0;i<2;i++){
            Map<Integer,List<User>> map = new HashMap<>();
            for(int j=0;j<2;j++){
                List<User> list1=new ArrayList<>();
                for (int k=0;k<3;k++){
                    User user=new User();
                    user.setId(101+k);
                    user.setNick("张"+k);
                    user.setPhone("1732007"+k);
                    user.setAddress("上海市闵行区"+k);
                    list1.add(user);
                }
                map.put(j,list1);
            }
            list.add(map);
        }
        model.addAttribute("list",list);
        return "eachAll";
    }
}
